﻿package mortal.game.view.medal.view
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.mui.controls.*;
    import com.mui.manager.*;
    import extend.language.*;
    import flash.events.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.medal.*;

    public class MedalRender extends ToolTipSprite
    {
        private var _name:GTextFiled;
        private var _medalIcon:GImageBitmap;
        private var _selectBg:GImageBitmap;
        private var _medalLvIcon:GImageBitmap;
        private var _medalLevel:BitmapNumberText;
        private var _upgradeBtn:GButton;
        private var _info:TMedal;
        private var _item:ItemData;

        public function MedalRender()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.mouseEnabled = true;
            this.mouseChildren = true;
            this.buttonMode = true;
            this._name = UIFactory.gTextField("", 6, 10, 120, 20, this, GlobalStyle.textFormatLv.center());
            this._selectBg = UIFactory.gImageBitmap(ImagesConst.medal_overSkin, 7, 30, this);
            this._medalIcon = UIFactory.gImageBitmap("", 30, 44, this);
            this._medalLvIcon = UIFactory.gImageBitmap(ImagesConst.challengeCopyNewTxt_11, 75, 100, this);
            this._medalLevel = UIFactory.bitmapNumberText(102, 101, "FightInfoNum.png", 12, 15, -1, this);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._name.dispose(param1);
            this._name = null;
            this._medalIcon.dispose(param1);
            this._medalIcon = null;
            this._selectBg.dispose(param1);
            this._selectBg = null;
            this._medalLvIcon.dispose(param1);
            this._medalLvIcon = null;
            this._medalLevel.dispose(param1);
            this._medalLevel = null;
            if (this._upgradeBtn)
            {
                this._upgradeBtn.dispose(param1);
                this._upgradeBtn = null;
            }
            this._info = null;
            this._item = null;
            super.disposeImpl(param1);
            return;
        }// end function

        private function addBtn() : void
        {
            if (this._upgradeBtn == null)
            {
                this._upgradeBtn = UIFactory.gButton(Language.getString(35405), 40, 116, 50, 25, this, "RedButton");
                this._upgradeBtn.configEventListener(MouseEvent.CLICK, this.upgradeMedal);
                EffectManager.glowFilterReg(this._upgradeBtn);
            }
            return;
        }// end function

        private function removeBtn() : void
        {
            if (this._upgradeBtn)
            {
                EffectManager.glowFilterUnReg(this._upgradeBtn);
                this._upgradeBtn.dispose();
                this._upgradeBtn = null;
            }
            return;
        }// end function

        private function upgradeMedal(event:MouseEvent) : void
        {
            GameProxy.packProxy.useItem(this._item.uid, this._item.itemCode, 1);
            return;
        }// end function

        private function updateBtn(param1:Object = null) : void
        {
            var _loc_3:Boolean = false;
            var _loc_4:TMedal = null;
            var _loc_5:Array = null;
            var _loc_6:TMedal = null;
            var _loc_7:ItemData = null;
            var _loc_2:* = Cache.instance.pack.backPackCache.getItemsByGroupCategoryAndType(EGroup._EGroupProp, EProp._EPropProp, EPropType._EPropTypeMedal);
            if (Cache.instance.medal.getOwnMedalByCatagoryAndType(this._info.catagory, this._info.type))
            {
                _loc_5 = MedalConfig.instance.medcatagoryDic[this._info.catagory][this._info.type];
                for each (_loc_6 in _loc_5)
                {
                    
                    if (_loc_6.typeLevel - this._info.typeLevel == 1)
                    {
                        _loc_4 = _loc_6;
                        break;
                    }
                }
            }
            else
            {
                _loc_4 = this._info;
            }
            if (_loc_4)
            {
                for each (_loc_7 in _loc_2)
                {
                    
                    if (_loc_7.itemInfo.effect == _loc_4.code)
                    {
                        _loc_3 = true;
                        this._item = _loc_7;
                        break;
                    }
                }
            }
            else
            {
                _loc_3 = false;
            }
            if (_loc_3)
            {
                this.addBtn();
            }
            else
            {
                this.removeBtn();
            }
            return;
        }// end function

        public function update(param1:TMedal) : void
        {
            this._info = param1;
            this._name.htmlText = MedalUtil.getMedalNameByTmedal(this._info);
            this._medalIcon.imgUrl = MedalUtil.getMedalIconUrlByTmedal(this._info, 1);
            if (Cache.instance.medal.getOwnMedalByCatagoryAndType(this._info.catagory, this._info.type))
            {
                this._medalIcon.filters = [];
                this._medalLevel.text = "" + this._info.typeLevel;
                this._medalLvIcon.visible = true;
            }
            else
            {
                this._medalIcon.filters = [FilterConst.colorFilter2];
                this._medalLvIcon.visible = false;
                this._medalLevel.text = "";
            }
            this.updateBtn();
            return;
        }// end function

    }
}
